//
//  HomePage.m
//  Exercise
//
//  Created by zhouxiaobo on 15/11/11.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import "HomePage.h"
#import "ModelWin.h"
#import "ModelWin2.h"

@implementation HomePage


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2 - 80, 250, 160, 40)];
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"模式窗－代理回调" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(showModel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2 - 80, 300, 160, 40)];
    btn1.backgroundColor = [UIColor greenColor];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn1 setTitle:@"模式窗－通知回调" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(showModel2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    
    UILabel *lable = [[UILabel alloc]init];
    lable.tag = 1001;
    lable.frame = CGRectMake(screenWidth/2 - 50, 200, 100, 30);
    lable.text = @"Hello World";
    [self.view addSubview:lable];
    
    UIButton *alertBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth/4 - 75, 50, 150, 30)];
    alertBtn1.backgroundColor = [UIColor greenColor];
    [alertBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [alertBtn1 setTitle:@"Alert" forState:UIControlStateNormal];
    [alertBtn1 addTarget:self action:@selector(alert1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alertBtn1];
    
    UIButton *alertBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth/4 + (screenWidth/2)- 75, 50, 150, 30)];
    alertBtn2.backgroundColor = [UIColor greenColor];
    [alertBtn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [alertBtn2 setTitle:@"ActionSheet" forState:UIControlStateNormal];
    [alertBtn2 addTarget:self action:@selector(alert2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alertBtn2];
    
    
    //利用通知中心来监控方向变动
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrg:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

-(void) alert1 {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"DemoAlert" message:@"This is a demo alert" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void) alert2 {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"DemoAlert" message:@"This is a demo alert" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"执行" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了执行");
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)deviceOrg:(NSNotification *) notication{
    UIDevice *device = (UIDevice *)[notication object];
    NSLog(@"orientation:%d",device.orientation);
}

- (void)showModel {
    NSLog(@"Click button");
    ModelWin *mWin = [[ModelWin alloc]init];
    mWin.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    mWin.delegate = self;
    
    [self presentViewController:mWin animated:YES completion:^{
        NSLog(@"打开窗口后，回调了");
    }];
}

-(void)showModel2{
    ModelWin2 *mWin = [[ModelWin2 alloc]init];
    mWin.modalTransitionStyle  = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:mWin animated:YES completion:^{
        NSLog(@"打开窗口2后，回调了");
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLableText2:) name:@"changeLabelValue" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//是否支持旋转
-(BOOL)shouldAutorotate{
    return YES;
}

//支持那些方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

//旋转时做的事
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(UIInterfaceOrientationLandscapeLeft == toInterfaceOrientation){
        NSLog(@"%f",duration);
    }
}

- (void) changeLableText:(NSString *)text {
    UILabel *lable = [self.view viewWithTag:1001];
    lable.text = text;
}

- (void) changeLableText2:(NSNotification *)notificate {
    UILabel *lable = [self.view viewWithTag:1001];
    NSString *text = notificate.object;
    lable.text = text;
}

@end
